package com.kangfu.api.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.kangfu.api.interceptor.AuthorizationInterceptor;
import com.kangfu.api.service.ILessonService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.common.model.AjaxResult;
import com.kangfu.db.model.vo.LessonInfoVo;
import com.kangfu.db.model.vo.UserPrescriptionLessonWeekVo;
import com.kangfu.db.model.vo.UserPrescriptionLessonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/lesson")
public class LessonController {

    @Autowired
    public ILessonService lessonService;

    /**
     * 首页获取某一天的推荐课程
     * @param userId
     * @param date
     * @return
     */
    @GetMapping("")
    public AjaxResult getLesson(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "date", defaultValue = "") String date) {
        LocalDate requstDate = LocalDateTimeUtil.parseDate(date);
        if (requstDate == null) {
            requstDate = LocalDate.now();
        }
        System.out.println(requstDate);
        List<UserPrescriptionLessonVo> list = lessonService.selectUserPrescriptionLessonByDate(userId, requstDate);
        return AjaxResult.success(list);
    }

    /**
     * 获取历史推荐课程
     * @param userId
     * @param page
     * @return
     */
    @GetMapping("/history")
    public AjaxResult getLessonHistory(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId,@RequestParam(name = "page", defaultValue = "1") Integer page) {
        List<UserPrescriptionLessonWeekVo> list = lessonService.getLessonHistory(userId,page);
        return AjaxResult.success(list);
    }


    /**
     * 获取某个课程的详情
     * @param recommendId
     * @return
     */
    @GetMapping("/info")
    public AjaxResult getLessonInfo(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "recommendId", defaultValue = "0") String recommendId) {
        if ("null".equals(recommendId) || "".equals(recommendId)) {
            throw new BusinessException("您的运动处方暂未生成，请耐心等待");
        }
        Integer recommendIdInt = Integer.parseInt(recommendId);
        LessonInfoVo lessonInfoVo = lessonService.getLessonInfo(userId, recommendIdInt);

        return AjaxResult.success(lessonInfoVo);
    }

    /**
     * 某个课程进行打卡
     * @param userId
     * @param recommendId
     * @return
     */
    @GetMapping("/checkin")
    public AjaxResult checkin(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "recommendId", defaultValue = "0") String recommendId) {
        if ("null".equals(recommendId) || "".equals(recommendId)) {
            throw new BusinessException("您的运动处方暂未生成，请耐心等待");
        }
        Integer recommendIdInt = Integer.parseInt(recommendId);
        LessonInfoVo lessonInfoVo = lessonService.getLessonInfo(userId, recommendIdInt);
        //打卡
        if (lessonInfoVo != null) {
            lessonService.checkin(userId, recommendIdInt);
        }
        return AjaxResult.success();
    }
}
